Travis James Smith

In [13]:
import tensorflow as tf

Problems 1 & 2

IMG_1382.jpg

Small error above -- in calculating the partial derivative of $f$ with respect to $x$, I wrote $\delta a / \delta f$ right before the equals sign when it should have been $\delta a / \delta x$.

In [34]:
x = tf.Variable(-2.0)
y = tf.Variable(1.0)
z = tf.Variable(3.0)

with tf.GradientTape() as tape:
    tape.watch(x)
    
    f = x * y + 2 * z + (1 / x)
    
    df_dx = tape.gradient(f,x)
print("df/dx: ", df_dx)

with tf.GradientTape() as tape:
    tape.watch(y)
    
    f = x * y + 2 * z + (1 / x)
    
    df_dy = tape.gradient(f,y)
print("df/dy: ", df_dy)

with tf.GradientTape() as tape:
    tape.watch(z)
    
    f = x * y + 2 * z + (1 / x)
    
    df_dz = tape.gradient(f,z)
print("df/dz: ", df_dz)
df/dx:  tf.Tensor(0.75, shape=(), dtype=float32)
df/dy:  tf.Tensor(-2.0, shape=(), dtype=float32)
df/dz:  tf.Tensor(2.0, shape=(), dtype=float32)

We can see from the above calculations that the Tensorflow outputs agree with our hand-calculated values.

Problems 3 & 4

IMG_1383.jpg

In [39]:
x = tf.Variable(1.0)
y = tf.Variable(-3.0)

with tf.GradientTape() as tape:
    tape.watch(x)
    
    f = (x + tf.sigmoid(-y)) / ((x - y)**2)
    
    df_dx = tape.gradient(f,x)
print("df/dx: ", df_dx)

with tf.GradientTape() as tape:
    tape.watch(y)
    
    f = (x + tf.sigmoid(-y)) / ((x - y)**2)
    
    df_dy = tape.gradient(f,y)
print("df/dy: ", df_dy)
df/dx:  tf.Tensor(0.0014820583, shape=(), dtype=float32)
df/dy:  tf.Tensor(0.0581944, shape=(), dtype=float32)

Our partial derivaties match up here as well!

The only small caveat is that they're both off by roughly $.0002$, which is reasonable given that we did some rounding with our hand-calculatons.

In [ ]: